package zsc.nine.onlinemall.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import zsc.nine.onlinemall.entity.Feedback;
import zsc.nine.onlinemall.service.FeedbackService;

import java.util.List;


@RequestMapping("/Feedback")
@RestController
public class FeedbackController {

    @Autowired
    FeedbackService feedbackService;
    //用户增加反馈
    @PostMapping("/AddFeedback")
    public String AddFeedback(@RequestParam(value = "Problem",required = false) String problem,
                              @RequestParam(value = "Content", required = false) String content,
                              @RequestParam(value = "UserEmail", required = false) String userEmail,
                              @RequestParam(value = "UserName", required = false) String userName){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("AddFeedbackResult", "Fail");

        /*后端校验数据合法性*/
        if (problem == null || content == null || userEmail == null ||userName == null) {
            jsonObject.put("AddFeedbackResult", "Parameter-Null");

        } else if (problem.trim().isEmpty()
                || content.trim().isEmpty()
                || userEmail.trim().isEmpty()
                || userName.trim().isEmpty()) {
            jsonObject.put("AddFeedbackResult", "Empty");

        } else {
            if(feedbackService.addFeedback(problem,content,userEmail,userName)) {
                jsonObject.put("AddFeedbackResult", "Success");
            }
        }
        return jsonObject.toJSONString();
    }


    //管理员（或者用户）删除反馈
    @PostMapping("/DeleteFeedback")
    public String DeleteFeedback(@RequestParam(value = "Id",required = false) String id){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("DeleteFeedbackResult", "Fail");

        /*后端校验数据合法性*/
        if (id == null) {
            jsonObject.put("DeleteFeedbackResult", "Parameter-Null");

        } else if (id.trim().isEmpty()) {
            jsonObject.put("DeleteFeedbackResult", "Empty");
        } else {
            if(feedbackService.deleteFeedback(Integer.parseInt(id))) {
                jsonObject.put("DeleteFeedbackResult", "Success");
            }
        }
        return jsonObject.toJSONString();
    }

    //管理员更改反馈状态(标记已读)
    @PostMapping("/UpdateOnRead")
    public String UpdateOnRead(@RequestParam(value = "Id",required = false) String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UpdateOnReadResult", "Fail");

        /*后端校验数据合法性*/
        if (id == null) {
            jsonObject.put("UpdateOnReadResult", "Parameter-Null");

        } else if (id.trim().isEmpty()) {
            jsonObject.put("UpdateOnReadResult", "Empty");
        } else {
            if(feedbackService.updateRead(Integer.parseInt(id),true)) {
                jsonObject.put("UpdateOnReadResult", "Success");
            }
        }
        return jsonObject.toJSONString();
    }
    //管理员更改反馈状态(标记未读)
    @PostMapping("/UpdateOffRead")
    public String UpdateOffRead(@RequestParam(value = "Id",required = false) String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("UpdateOffReadResult", "Fail");

        /*后端校验数据合法性*/
        if (id == null) {
            jsonObject.put("UpdateOffReadResult", "Parameter-Null");

        } else if (id.trim().isEmpty()) {
            jsonObject.put("UpdateOffReadResult", "Empty");
        } else {
            if(feedbackService.updateRead(Integer.parseInt(id),false)) {
                jsonObject.put("UpdateOffReadResult", "Success");
            }
        }
        return jsonObject.toJSONString();
    }

    //管理员后台查看所有(反馈)
    @PostMapping("/FindAllFeedback")
    public String findAllFeedback(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("FindAllFeedbackResult", "Fail");
        List<Feedback> feedbackList = feedbackService.findAll();
        jsonObject.put("FindAllFeedbackResult", "Success");
        jsonObject.put("FindAllFeedbackObjectResult", feedbackList);

        return jsonObject.toJSONString();
    }

    //用户查看自己所提交的所有反馈（前台调用）
    @PostMapping("/FindAllFeedbackByEmail")
    public String findAllFeedbackByEmail(@RequestParam(value = "UserEmail",required = false) String userEmail){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("FindAllFeedbackByEmailResult", "Fail");

        /*后端校验数据合法性*/
        if (userEmail == null) {
            jsonObject.put("FindAllFeedbackByEmailResult", "Parameter-Null");
        } else if (userEmail.trim().isEmpty()) {
            jsonObject.put("FindAllFeedbackByEmailResult", "Empty");
        } else {
            List<Feedback> feedbackList = feedbackService.findAllByEmail(userEmail);
            jsonObject.put("FindAllFeedbackByEmailResult", "Success");
            jsonObject.put("FindAllFeedbackByEmailObjectResult",feedbackList);
        }
        return jsonObject.toJSONString();
    }
}
